from django.db import models
from django.contrib.auth.models import User


class Book(models.Model):
    CATEGORY_CHOICES = [
        ('CS', '计算机科学'),
        ('MA', '数学'),
        ('PH', '物理'),
        ('EN', '英语'),
        ('HI', '历史'),
    ]

    STATUS_CHOICES = [
        ('available', '可借阅'),
        ('borrowed', '已借出'),
        ('reserved', '已预订'),
    ]

    title = models.CharField(max_length=200, verbose_name="书名")
    author = models.CharField(max_length=100, verbose_name="作者")
    publisher = models.CharField(max_length=100, verbose_name="出版社")
    publish_date = models.DateField(verbose_name="出版日期")
    isbn = models.CharField(max_length=20, unique=True, verbose_name="ISBN")
    category = models.CharField(max_length=2, choices=CATEGORY_CHOICES, verbose_name="分类")
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='available', verbose_name="状态")
    cover = models.ImageField(upload_to='covers/', blank=True, null=True, verbose_name="封面")
    description = models.TextField(blank=True, verbose_name="描述")

    class Meta:
        verbose_name = "图书"
        verbose_name_plural = "图书"

    def __str__(self):
        return self.title


class BorrowRecord(models.Model):
    book = models.ForeignKey(Book, on_delete=models.CASCADE, verbose_name="图书")
    borrower = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="借阅人")
    borrow_date = models.DateField(auto_now_add=True, verbose_name="借阅日期")
    return_date = models.DateField(blank=True, null=True, verbose_name="归还日期")
    due_date = models.DateField(verbose_name="应还日期")
    is_returned = models.BooleanField(default=False, verbose_name="是否归还")

    class Meta:
        verbose_name = "借阅记录"
        verbose_name_plural = "借阅记录"

    def __str__(self):
        return f"{self.borrower.username}借阅{self.book.title}"